package com.yhsoft.buyer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhsoft.buyer.controller.request.*;
import com.yhsoft.buyer.controller.response.CartOutVo;
import com.yhsoft.buyer.controller.response.CheckOutVo;
import com.yhsoft.buyer.entity.BuyerCartSku;
import com.yhsoft.buyer.service.IBuyerCartService;
import com.yhsoft.buyer.service.IBuyerCartSkuService;
import com.yhsoft.buyer.service.IBuyerOrderService;
import com.yhsoft.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-26
 */
@Api(tags = {"G-wx-购物车接口"})
@RestController
@RequestMapping("/wxApi/buyerCart")
public class BuyerCartController {
    @Autowired
    private IBuyerCartService iBuyerCartService;

    @Autowired
    private IBuyerCartSkuService iBuyerCartSkuService;

    @Autowired
    private IBuyerOrderService iBuyerOrderService;

    @ApiOperation("加入购物车")
    @PostMapping("/addToCart")
    public Result<?> addToCart(@Valid @RequestBody CartInVo inVo) {
        Result<?> result = iBuyerCartService.addToCart(inVo, false);
        return result;
    }

    @ApiOperation("查看购物车列表")
    @GetMapping("/getCart")
    public Result<List<CartOutVo>> getCart() {
        List<CartOutVo> list = iBuyerCartService.getCart();
        return Result.success(list);
    }

    @ApiOperation("立即购买")
    @PostMapping("/fastadd")
    public Result<?> fastadd(@Valid @RequestBody FastAddInVo inVo) {
        String cartId = StringUtils.substringAfter(iBuyerCartService.addToCart(inVo.getCartInVo(),true).getMessage(),"：");
        CheckoutInVo checkout = new CheckoutInVo();
        checkout.setCartId(Integer.parseInt(cartId));
        checkout.setAddressId(inVo.getAddressId());
        return checkout(checkout);
    }

    @ApiOperation("修改购物车商品数量")
    @PostMapping("/updateGoodsNum")
    public Result<?> updateGoodsNum(@Valid @RequestBody UpdateGoodsNumInVo inVo) {
        BuyerCartSku buyerCartSku = iBuyerCartSkuService.getById(inVo.getCartSkuId());
        if(buyerCartSku == null){
            return Result.success("购物车sku子项表记录为空！");
        }
        buyerCartSku.setSkuQuantity(inVo.getSkuQuantity());
        iBuyerCartSkuService.getBaseMapper().updateById(buyerCartSku);
        return Result.success("修改成功！");
    }

    @ApiOperation("购物车商品删除")
    @PostMapping("/delete")
    public Result<?> delete(@Valid @RequestBody SkuIdInVo inVo) {
        iBuyerCartSkuService.getBaseMapper().deleteById(inVo.getCartSkuId());

        // 判断是否是最后一个sku子项，是的话删除购物车主表
        LambdaQueryWrapper<BuyerCartSku> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BuyerCartSku::getCartId, inVo.getCartId());
        List<BuyerCartSku> list = iBuyerCartSkuService.list(wrapper);
        if(list.size() == 0){
            iBuyerCartService.getBaseMapper().deleteById(inVo.getCartId());
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("购物车下单")
    @PostMapping("/checkout")
    public Result<CheckOutVo> checkout(@Valid @RequestBody CheckoutInVo inVo) {
        CheckOutVo outVo = iBuyerOrderService.checkout(inVo.getCartId(), inVo.getAddressId());
        if (outVo == null) {
            Result.fail("下单失败！");
        }
        return Result.success(outVo);
    }
}
